package com.shoppingmall.activity.classification;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.BarUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseActivity;
import com.google.android.material.tabs.TabLayout;
import com.shoppingmall.R;
import com.shoppingmall.widget.RecycleGridDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * liangwanbo
 * 分类商品详情
 */
public class ClassGoodInfoActivity extends BaseActivity {


    @BindView(R.id.banner_car_pic_list)
    ImageView bannerCarPicList;
    @BindView(R.id.re_tupian)
    RelativeLayout reTupian;
    @BindView(R.id.goodmoney)
    TextView goodmoney;
    @BindView(R.id.re_sp)
    RelativeLayout reSp;
    @BindView(R.id.re_zanwupj)
    RelativeLayout reZanwupj;
    @BindView(R.id.image_freight)
    ImageView imageFreight;
    @BindView(R.id.freight_money)
    TextView freightMoney;
    @BindView(R.id.image_salenumber)
    ImageView imageSalenumber;
    @BindView(R.id.salenumber)
    TextView salenumber;
    @BindView(R.id.image_seeknumber)
    ImageView imageSeeknumber;
    @BindView(R.id.seeknumber)
    TextView seeknumber;
    @BindView(R.id.lin_servise)
    LinearLayout linServise;
    @BindView(R.id.re_choice)
    RelativeLayout reChoice;
    @BindView(R.id.goto_luck)
    ImageView gotoLuck;
    @BindView(R.id.shoplogo)
    ImageView shoplogo;
    @BindView(R.id.shopname)
    TextView shopname;
    @BindView(R.id.adress)
    TextView adress;
    @BindView(R.id.image_describe)
    ImageView imageDescribe;
    @BindView(R.id.tv_describe)
    TextView tvDescribe;
    @BindView(R.id.describe_number)
    TextView describeNumber;
    @BindView(R.id.re_describe)
    RelativeLayout reDescribe;
    @BindView(R.id.image_service_attitude)
    ImageView imageServiceAttitude;
    @BindView(R.id.tv_service_attitude)
    TextView tvServiceAttitude;
    @BindView(R.id.service_nimber)
    TextView serviceNimber;
    @BindView(R.id.re_service)
    RelativeLayout reService;
    @BindView(R.id.image_speed)
    ImageView imageSpeed;
    @BindView(R.id.tv_speed)
    TextView tvSpeed;
    @BindView(R.id.speed_number)
    TextView speedNumber;
    @BindView(R.id.re_speed)
    RelativeLayout reSpeed;
    @BindView(R.id.bnt_all_good)
    Button bntAllGood;
    @BindView(R.id.go_shop)
    Button goShop;
    @BindView(R.id.re_shop)
    RelativeLayout reShop;
    @BindView(R.id.image_lable)
    ImageView imageLable;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.image_more)
    ImageView imageMore;
    @BindView(R.id.re_more)
    RelativeLayout reMore;
    @BindView(R.id.commentReceyview)
    RecyclerView commentReceyview;
    @BindView(R.id.re_comment)
    RelativeLayout reComment;
    @BindView(R.id.tv_recommend)
    TextView tvRecommend;
    @BindView(R.id.re_tuijian)
    RelativeLayout reTuijian;
    @BindView(R.id.recommendReceyview)
    RecyclerView recommendReceyview;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.re_infos)
    RelativeLayout reInfos;
    @BindView(R.id.MywebView)
    WebView MywebView;
    @BindView(R.id.your_fragment)
    FrameLayout yourFragment;
    @BindView(R.id.nsv)
    NestedScrollView nsv;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tl_car_detail)
    TabLayout tlCarDetail;
    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.image_collection)
    ImageView imageCollection;
    @BindView(R.id.re_tob)
    RelativeLayout reTob;
    @BindView(R.id.buy)
    Button buy;
    @BindView(R.id.sharemoey)
    Button sharemoey;
    @BindView(R.id.imahe_redshop)
    ImageView imaheRedshop;
    @BindView(R.id.re_bottom_shop)
    RelativeLayout reBottomShop;
    @BindView(R.id.imahe_kf)
    ImageView imaheKf;
    @BindView(R.id.re_kf)
    RelativeLayout reKf;
    @BindView(R.id.imahe_shop_car)
    ImageView imaheShopCar;
    @BindView(R.id.re_shopcar)
    RelativeLayout reShopcar;
    @BindView(R.id.shangpinbut)
    RelativeLayout shangpinbut;
    private boolean scrollParent = true;
    private BaseQuickAdapter<String, BaseViewHolder> mAdapter;

    private BaseQuickAdapter<String, BaseViewHolder> commendAdapter;

    @Override
    protected int initLayout() {
        return R.layout.houbaogoodinfolayout;
    }

    @Override
    protected void initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        //BarUtils.setStatusBarLightMode(this, false);
    }

    @Override
    protected void initData() {
        reTob.getBackground().setAlpha(0);
        tlCarDetail.setAlpha(0);
        ivBack.setAlpha(0f);
        imageBack.setAlpha(1f);
        imageCollection.setAlpha(1f);
        tlCarDetail.addTab(tlCarDetail.newTab().setText("商品"));
        tlCarDetail.addTab(tlCarDetail.newTab().setText("评价"));
        tlCarDetail.addTab(tlCarDetail.newTab().setText("推荐"));
        tlCarDetail.addTab(tlCarDetail.newTab().setText("详情"));
        scrollParent = false;

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            list.add("");
        }
        mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_goodinfo_comment, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, String s) {

            }
        };

        commentReceyview.setLayoutManager(new LinearLayoutManager(this));
        commentReceyview.setAdapter(mAdapter);

        List<String> listcommend = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            listcommend.add("");
        }

        commendAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_recommend, listcommend) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, String s) {

            }
        };
        LinearLayoutManager linManger = new LinearLayoutManager(this);
        linManger.setOrientation(LinearLayoutManager.HORIZONTAL);
        recommendReceyview.setLayoutManager(linManger);
        recommendReceyview.addItemDecoration(new RecycleGridDivider());
        recommendReceyview.setAdapter(commendAdapter);

    }

    @Override
    protected void initEvent() {

        tlCarDetail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (!scrollParent) {
                    int tabHeight = tlCarDetail.getHeight();
                    Log.e("OnTabSelected", "tabId=" + tab.getPosition());
                    switch (tab.getPosition()) {
                        case 0: {
                            nsv.scrollTo(0, 0);

                        }
                        break;
                        case 1: {
                            int targetY = (int) reComment.getY();
                            nsv.scrollTo(0, targetY - tabHeight);

                        }
                        break;
                        case 2: {
                            int targetY = (int) reTuijian.getY();
                            nsv.scrollTo(0, targetY - tabHeight);

                        }
                        break;

                        case 3: {
                            int targetY = (int) reInfos.getY();
                            nsv.scrollTo(0, targetY - tabHeight);
                        }
                        break;

                    }
                    scrollParent = false;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @SuppressLint("Range")
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //当滑动到金融方案一栏时显示导航条
                int tabHeight = reTob.getHeight();
                Log.e("tabHeight", tabHeight + "");
                Log.e("scrollY", scrollY + "");

                if (scrollY <= 0) {   //设置标题的背景颜色
                    reTob.getBackground().setAlpha(0);
                    tlCarDetail.setAlpha(0);
                    ivBack.setAlpha(0f);
                    imageBack.setAlpha(1f);
                    imageCollection.setAlpha(1f);
                    //reTob.setBackgroundColor(Color.argb((int) 0, 239, 86, 112));
                } else if (scrollY > 0 && scrollY <= tabHeight) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) scrollY / tabHeight;
                    float alpha = (255 * scale);
                    reTob.getBackground().setAlpha((int) alpha);
                    tlCarDetail.setAlpha(scale);
                    ivBack.setAlpha(scale);
                    imageBack.setAlpha(scale);
                    imageCollection.setAlpha(scale);
                    // reTob.setBackgroundColor(Color.argb((int) alpha, 239, 86, 112));
                } else {    //滑动到banner下面设置普通颜色
                    //reTob.setBackgroundColor(Color.argb((int) 255, 239, 86, 112));
                    reTob.getBackground().setAlpha(255);
                    tlCarDetail.setAlpha(1f);
                    ivBack.setAlpha(1f);
                    imageBack.setAlpha(0f);
                    imageCollection.setAlpha(0f);

                }

                //得到各个标签再scrollView中的y坐标位置
                float target1 = reTupian.getY() - tlCarDetail.getHeight();
                float target2 = reComment.getY() - tlCarDetail.getHeight();
                float target3 = reTuijian.getY() - tlCarDetail.getHeight();
                float target4 = reInfos.getY() - tlCarDetail.getHeight();
                scrollParent = true;
                if (scrollY >= target1 && scrollY < target2) {
                    if (!tlCarDetail.getTabAt(0).isSelected()) {
                        tlCarDetail.getTabAt(0).select();
                    }
                } else if (scrollY >= target2 && scrollY < target3) {
                    if (!tlCarDetail.getTabAt(2).isSelected()) {
                        tlCarDetail.getTabAt(2).select();
                    }
                } else if (scrollY >= target3 && scrollY < target4) {
                    if (!tlCarDetail.getTabAt(1).isSelected()) {
                        tlCarDetail.getTabAt(1).select();
                    }
                } else if (scrollY >= target4) {
                    if (!tlCarDetail.getTabAt(3).isSelected()) {
                        tlCarDetail.getTabAt(3).select();
                    }
                }
                scrollParent = false;
            }
        });

    }



}
